// User Controller - 控制器层

const userService = require('./service')

/**
 * 用户注册
 */
exports.register = async (req, res) => {
  try {
    const { username, password } = req.body
    await userService.register(username, password)
    res.success({ msg: '注册用户成功！' })
  } catch (error) {
    res.error(error.message || error)
  }
}

/**
 * 用户登录
 */
exports.login = async (req, res) => {
  try {
    const { username, password } = req.body
    const result = await userService.login(username, password)

    res.success({ data: result, msg: '登录成功！' })
  } catch (error) {
    res.error(error.message || error)
  }
}

/**
 * 根据用户 ID 查询用户信息
 */
exports.userInfo = async (req, res) => {
  try {
    const id = req.params.id
    const result = await userService.getUserInfo(id)
    res.success({ data: result, msg: '获取用户信息成功！' })
  } catch (error) {
    res.error(error.message || error)
  }
}
